"""
基础页面类
提供通用的页面操作方法
"""

from abc import ABC, abstractmethod
from typing import Optional
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

from utils.driver_manager import DriverManager


class BasePage(ABC):
    """基础页面类"""
    
    def __init__(self, driver_manager: DriverManager):
        """
        初始化基础页面
        
        Args:
            driver_manager: 驱动管理器
        """
        self.driver_manager = driver_manager
        self.driver = driver_manager.driver
    
    def wait_for_element(self, locator: str, value: str, timeout: int = 10):
        """
        等待元素出现
        
        Args:
            locator: 定位方式
            value: 定位值
            timeout: 超时时间
            
        Returns:
            元素对象
        """
        return self.driver_manager.find_element(locator, value, timeout)
    
    def wait_for_elements(self, locator: str, value: str, timeout: int = 10):
        """
        等待多个元素出现
        
        Args:
            locator: 定位方式
            value: 定位值
            timeout: 超时时间
            
        Returns:
            元素列表
        """
        return self.driver_manager.find_elements(locator, value, timeout)
    
    def click(self, locator: str, value: str, timeout: int = 10) -> bool:
        """
        点击元素
        
        Args:
            locator: 定位方式
            value: 定位值
            timeout: 超时时间
            
        Returns:
            是否成功点击
        """
        return self.driver_manager.click_element(locator, value, timeout)
    
    def input_text(self, locator: str, value: str, text: str, timeout: int = 10) -> bool:
        """
        输入文本
        
        Args:
            locator: 定位方式
            value: 定位值
            text: 要输入的文本
            timeout: 超时时间
            
        Returns:
            是否成功输入
        """
        return self.driver_manager.input_text(locator, value, text, timeout)
    
    def get_text(self, locator: str, value: str, timeout: int = 10) -> str:
        """
        获取元素文本
        
        Args:
            locator: 定位方式
            value: 定位值
            timeout: 超时时间
            
        Returns:
            元素文本
        """
        return self.driver_manager.get_element_text(locator, value, timeout)
    
    def is_element_present(self, locator: str, value: str, timeout: int = 10) -> bool:
        """
        检查元素是否存在
        
        Args:
            locator: 定位方式
            value: 定位值
            timeout: 超时时间
            
        Returns:
            元素是否存在
        """
        return self.driver_manager.is_element_present(locator, value, timeout)
    
    def take_screenshot(self, filename: str = None) -> str:
        """
        截图
        
        Args:
            filename: 文件名
            
        Returns:
            截图文件路径
        """
        return self.driver_manager.take_screenshot(filename)
    
    def swipe_up(self, duration: int = 1000):
        """向上滑动"""
        if not self.driver:
            return
        
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.5
        start_y = size['height'] * 0.8
        end_x = size['width'] * 0.5
        end_y = size['height'] * 0.2
        
        self.driver_manager.swipe(start_x, start_y, end_x, end_y, duration)
    
    def swipe_down(self, duration: int = 1000):
        """向下滑动"""
        if not self.driver:
            return
        
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.5
        start_y = size['height'] * 0.2
        end_x = size['width'] * 0.5
        end_y = size['height'] * 0.8
        
        self.driver_manager.swipe(start_x, start_y, end_x, end_y, duration)
    
    def swipe_left(self, duration: int = 1000):
        """向左滑动"""
        if not self.driver:
            return
        
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.8
        start_y = size['height'] * 0.5
        end_x = size['width'] * 0.2
        end_y = size['height'] * 0.5
        
        self.driver_manager.swipe(start_x, start_y, end_x, end_y, duration)
    
    def swipe_right(self, duration: int = 1000):
        """向右滑动"""
        if not self.driver:
            return
        
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.2
        start_y = size['height'] * 0.5
        end_x = size['width'] * 0.8
        end_y = size['height'] * 0.5
        
        self.driver_manager.swipe(start_x, start_y, end_x, end_y, duration)
    
    @abstractmethod
    def is_page_loaded(self) -> bool:
        """
        检查页面是否加载完成
        
        Returns:
            页面是否加载完成
        """
        pass
    
    @abstractmethod
    def wait_for_page_load(self, timeout: int = 30) -> bool:
        """
        等待页面加载完成
        
        Args:
            timeout: 超时时间
            
        Returns:
            页面是否加载完成
        """
        pass 